import Taro from '@tarojs/taro';
// import { Current } from '@tarojs/taro';
import { accredit } from '@api/login';
import Toast from '@components/vant/toast/toast';
export function login() {
  const that = this;
  var gsid = Taro.getStorageSync('gsid');
  Taro.showLoading({
    title: '授权中',
  });
  Taro.login({
    success: function(res) {
      if (res.code) {
        Taro.request({
          url: 'https://xxyg.daxinxin.vip/api.php/home/index/getwxuser',
          data: {
            code: res.code,
          },
          success: function(e) {
            if (e.data.code == 0) {
              //_that.loadding(false);
              Toast(e.data.data.msg);
              Taro.hideLoading();
            } else {
              Taro.setStorageSync('session_id', e.data.data.openid);
              const session_id = e.data.data.openid;
               console.log("我走到这一步");
              Taro.getSetting({
                success(res) {
                    console.log(res,"我走到这一步1");
                  if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    Taro.getUserInfo({
                      success: function(res) {
                        const { nickName, avatarUrl } = res.userInfo;
                        accredit({
                          session_id: session_id,
                          nick_name: nickName,
                          xcxavatarurl: avatarUrl,
                          gsid: gsid,
                        }).then(res => {
                          Taro.hideLoading();
                          console.log(!res.code,'授权成功2424');
                          if (!res.code) {
                            // Taro.showToast({
                            //     title: '授权成功',
                            //     duration: 2000
                            // });
                        
                          }
                        });
                      },
                    });
                  }
                },
              });
            }
          },
        });
      } else {
        Toast.fail('授权失败,返回首页');
        Taro.reLaunch({
          url: '/pages/index/index',
        });
      }
    },
  });
}
